package com.xy.server.database;

import com.xy.server.entity.CaptureProfile;
import com.xy.server.entity.MosaicTemplate;
import com.xy.server.entity.Profile;
import com.xy.server.util.ServerUtil;
import org.apache.ibatis.annotations.*;

import java.io.File;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Mapper
public interface FaceMapper {

    @Select("select value from dictionary where name =#{name}")
    String getDicValueByName(@Param("name") String name);


    @Select("select value from dictionary where name =#{name}")
    String getDicStringByName(@Param("name") String name);

    @Update("update dictionary set value=value+1 where name=#{name}")
    void increaseDicValue(@Param("name") String name);

    @Update("update dictionary set value=#{value} where name=#{name}")
    void updateDicValue(@Param("name") String name,@Param("value") String value);

    @Insert("insert into capture_profile(origin_file_name,profile_file_name,emotion,red_count,green_count,blue_count,pixel_count,create_time) values(#{origin_file_name},#{profile_file_name},#{emotion},#{red_count},#{green_count},#{blue_count},#{pixel_count},#{create_time})")
    void addProfile(@Param("origin_file_name") String origin_file_name,
                                  @Param("profile_file_name") String profile_file_name,
                                  @Param("emotion") String emotion,
                                  @Param("red_count") int red_count,
                                  @Param("green_count") int green_count ,
                                  @Param("blue_count") int blue_count ,
                                  @Param("pixel_count") int pixel_count,
                                  @Param("create_time") long create_time);

    @Update("delete from mosaic_template")
    void clearMosaicTemplate();

    @Insert("insert into mosaic_template(x,y,width,height,red_count,green_count,blue_count,pixel_count,create_time) values(#{x},#{y},#{width},#{height},#{red_count},#{green_count},#{blue_count},#{pixel_count},#{create_time})")
    void addMosaicTemplate(@Param("x") int x,
                           @Param("y") int y,
                           @Param("width") int width,
                           @Param("height") int height,
                           @Param("red_count") int red_count,
                           @Param("green_count") int green_count ,
                           @Param("blue_count") int blue_count ,
                           @Param("pixel_count") int pixel_count,
                           @Param("create_time") long create_time);


    @Select("select * from capture_profile order by create_time desc limit #{count}")
    List<CaptureProfile> getProfileList(@Param("count") int count);

//    ProfileList<Profile> profileList = new ArrayList<Profile>();
//
//    Statement statement = connection.createStatement();
//
//        statement.setQueryTimeout(30);
//    ResultSet rs = statement.executeQuery("select * from capture_profile order by create_time desc limit " + count);
//        while (rs.next()) {
//
//        File file = new File(ServerUtil.ResPath + rs.getString("profile_file_name"));
//
//        if(file.exists()){
//            Profile profile = new Profile(
//                    file,
//                    rs.getString("origin_file_name"),
//                    rs.getString("emotion"),
//                    rs.getInt("red_count"),
//                    rs.getInt("green_count"),
//                    rs.getInt("blue_count"),
//                    rs.getInt("pixel_count"),
//                    0//rs.getLong("create_time")
//            );
//
//            profileList.add(profile);
//        }
//
//    }
//
//        return profileList;


    @Select("select * from mosaic_template ")
    List<MosaicTemplate> getMosaicTemplateList();
}
